package fr.utbm.tx52.galerie;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import fr.utbm.tx52.dataManagement.DataManager;
import fr.utbm.tx52.sharing.CommentAdder;



/**
 * Pick up d'une image de la galerie pour l'envoyer
 */
public class Galerie extends Activity {

	
	/**
	 * Identifiant de l'activité qui va prendre la photo
	 */
	private static final int ACTIVITY_SELECT_IMAGE = 1;

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		startActivityForResult(i, ACTIVITY_SELECT_IMAGE); 
	}

	


	/**
	 * Est appellée quand la selection de la photo a partager est terminée
	 */
	protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
	    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

	    switch(requestCode) { 
	    
	    case ACTIVITY_SELECT_IMAGE :
	    	
	        if(resultCode == RESULT_OK){ 
	        	
	            Uri selectedImage = imageReturnedIntent.getData();
	            String[] filePathColumn = {MediaStore.Images.Media.DATA};

	            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
	            cursor.moveToFirst();

	            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
	            String filePath = cursor.getString(columnIndex);
	            cursor.close();

				Intent i = new Intent();
				i.setClass(getApplicationContext(), CommentAdder.class);
				i.putExtra("photopath", filePath);
				startActivity(i);
				this.finish();
	        }
	    }
	    
	    this.finish();
	}

}

